/**
 * 
 */
package common.fengkai.filepost.conf;

import java.io.File;
import java.io.IOException;
import cfca.paperless.util.IPUtils;
import cfca.paperless.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description CFCA专用日志组件加载工具
 * @Author tiansong
 * @Date 2019-8-5
 * @CodeReviewer liujiancheng
 */
@Slf4j
public class CfcaLogbackInitializer {
    


    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        new CfcaLogbackInitializer().init();
        log.info("info");
    }

    /**
     * init
     * @throws Exception 
     */
    public void init() throws Exception {
        init(null, null);
    }

    /**
     * init
     * @throws Exception 
     */
    public void init(String externalConfigFilePath, String applicationName) throws Exception {
        
        try{
            // 参数配置文件的存放目录
            if (StringUtil.isEmpty(externalConfigFilePath)) {
                externalConfigFilePath = "./config/logback-cfca.xml";
            }
            System.out.println("externalConfigFilePath=" + externalConfigFilePath);
            
            File externalConfigFile = new File(externalConfigFilePath);
            if (!externalConfigFile.exists()) {
                throw new IOException("Logback External Config File Parameter does not reference a file that exists");
            }
            if (!externalConfigFile.isFile()) {
                throw new IOException("Logback External Config File Parameter exists, but does not reference a file");
            }
            if (!externalConfigFile.canRead()) {
                throw new IOException("Logback External Config File exists and is a file, but cannot be read.");
            }
            
            externalConfigFilePath = new File(externalConfigFilePath).getCanonicalPath();
            System.out.println("externalConfigFilePath.getCanonicalPath=" + externalConfigFilePath);

            String localIPString = "";
            try {
                localIPString = IPUtils.getLocalHostLANAddress();
                System.out.println("获取服务器IP操作成功.IP=" + localIPString);
            } catch (Exception e) {
                System.err.println("获取服务器IP操作失败." + e.getMessage());
                System.err.println(e);
                localIPString = "NOIP";
            }
            
            if (StringUtil.isEmpty(applicationName)) {
                applicationName =  "PaperlessClient-log-" + localIPString;
            }


            // 加载指定路径logback-cfca.xml
            System.out.println("registe logback-cfca start.");
            try{
                //LogbackInitializer.setLogback(externalConfigFilePath, applicationName);
                System.out.println("CFCA专用日志组件加载成功[logback-cfca.xml].logbackFilePath=" + externalConfigFilePath + " applicationName=" + applicationName);
            } catch (Exception e) {
                System.err.println("CFCA专用日志组件加载失败[logback-cfca.xml].logbackFilePath=" + externalConfigFilePath + " applicationName=" + applicationName + "." + e.getMessage());
                System.err.println(e);
            }
            System.out.println("registe logback-cfca end.");
            
       } catch (Exception e) {
           System.err.println(e);
           throw e;
       }
    }
}
